
"use strict";
angular.module('app.project').controller('Add_newProjectCtrl', function ($scope,$location, $stateParams, $http, $cookieStore) {

    $scope.infor = {}; // 项目基本信息mode
    $scope.pro_id = $location.search().pro_id;
    $scope.userid = $cookieStore.get('userid');
    $scope.type_obj = {};
    // 设备类型
    $http.get(baseurl+'/device_types').then(function(res){
        if (res.data.ret == 1) {
            $scope.types = res.data.info;
            if (res.data.info.length > 0) {
                $scope.first_type = res.data.info[0].id;
                for (var i = 0; i < res.data.info.length; i++) {
                    var obj = res.data.info[i];
                    $scope.type_obj[obj.id] = obj.name;
                }

            }
        }
    });
    // 获取该项目的信息
    if ($scope.pro_id) {
        $scope.delbtn = false;
        $http.get(baseurl+"/detail/project?id=="+$scope.pro_id).then(function(res){
            if (res.data.ret == 1) {
                $scope.infor = res.data.info;
            }
        })
    }else{
        $scope.delbtn = true;//隐藏
    }
    // 保存该项目
    $scope.save_infor = function () { // 点击保存// 判断是创建新项目还是编辑项目
        if ($scope.pro_id) {
            if($scope.infor.name && $scope.infor.project_num && $scope.infor.clientname){
                $http.put(baseurl+'/data/project?id=='+$scope.pro_id,$scope.infor).then(function(res){
                    // console.log(res.data);
                    if (res.data.ret == 1) {
                        window.location.href = "/#/myallprojects/";//编辑项目
                    }
                });
            }else{
                new Notify("fail","有空值未填写");
            }

        }else{
            if($scope.infor.name && $scope.infor.project_num && $scope.infor.clientname){
                $scope.infor.fid = $scope.userid;
                $http.post(baseurl+'/data/project',$scope.infor).then(function(res){
                    // console.log(res.data);
                    if (res.data.ret == 1) {
                        window.location.href = "/#/myallprojects/";//添加项目
                    }
                });
            }else{
                new Notify("fail","有空值未填写");
            }

        }

    };
    // 设备列表的获取
    $scope.cur_page = 1;
    $scope.cur_rows = 9;
    $scope.select = function () {
        var page_obj = {};
        page_obj.page = $scope.cur_page;
        page_obj.rows = $scope.cur_rows;
        var projectid = $scope.pro_id;
        var query = '?projectid=='+projectid;
        // console.log(page_obj);
        $http.post(baseurl+'/list/select/view_device_project'+query,page_obj).then(function(res){
            // console.log(res.data);
            if (res.data.ret == 1) {
                if (res.data.rows != null) {
                    for (var i = 0; i < res.data.rows.length; i++) {
                        var obj = res.data.rows[i];
                        obj.typename = $scope.type_obj[obj.type];
                        if (obj.status == '0') {
                            obj.statusname = '异常';
                            obj.class_status = 'status_error';
                        }else{
                            obj.statusname = '正常';
                            obj.class_status = 'status_normal';
                        }
                    }
                    if (res.data.rows < 1 && $scope.more_data == true) {
                        new Notify('info','没有更多数据了');
                    }else{
                        if ($scope.cur_page == 1){
                            $scope.datas = res.data.rows;
                        }else{
                            var a = $scope.datas;
                            var c = a.concat(res.data.rows);
                            $scope.datas = c;
                        }
                    }
                }
            }
        })
    }
    if ($scope.pro_id && $scope.pro_id != '') {
        $scope.select();
    }

    // 加载更多
    $scope.jiazai_more = function(){
        $scope.cur_page += 1;
        $scope.more_data = true;
        $scope.select();
    }
});